<?php
/**
 * Api Response Cache
 *
 * @author  Aman Kumar Jain <connect@amanjain.com>
 * @license Apache v2
 * @link    http://code.google.com/p/api-response-cache/
 * @version 0.1
 *
 */

class ApiResponseCache
{
    var $cache_directory = ".cache"; //location of cache directory
    var $cache_time = 600; //time(in seconds) to cache the response
    var $identifier, $method, $api_url, $data;
    var $curl_options = array();
   
    function __construct($identifier, $api_url, $data=array(), $method="GET")
    {
        $this->set_url($identifier, $api_url, $data, $method);
    }
    
    public function set_url($identifier, $api_url, $data=array(), $method="GET")
    {
        $this->identifier = md5($identifier).".cache";
        $this->api_url = $api_url;
        $this->data = $data;
        $this->method = $method;        
    }
    
    public function fetch_response()
    {
        if($response = $this->read_cache())
        {
            return $response;
        }
        $response = $this->get_response();
        $this->write_cache($response);
        return $response;
    }
    
    private function read_cache()
    {
        if(file_exists($this->cache_directory.DIRECTORY_SEPARATOR.$this->identifier) && (filemtime($this->cache_directory.DIRECTORY_SEPARATOR.$this->identifier) > (time() - $this->cache_time)))
        {
            return file_get_contents($this->cache_directory.DIRECTORY_SEPARATOR.$this->identifier);
        }
        return false;
    }
    
    private function write_cache($response)
    {
        file_put_contents($this->cache_directory.DIRECTORY_SEPARATOR.$this->identifier, $response);
    }

    private function get_response()
    {
        if(is_array($this->data))
        {
            $params = "";
            foreach($this->data as $key => $value)
            {
                $params .= "&{$key}=".urlencode($value);
            }
        }

        $curl = curl_init();
        if(is_array($this->data) && sizeOf($this->data)>=1)
        {
            if($this->method == "GET")
            {
                curl_setopt($curl, CURLOPT_URL, $this->api_url."?".substr($params, 1));
            }
            else
            {
                curl_setopt($curl, CURLOPT_URL, $this->api_url);
                curl_setopt($curl, CURLOPT_POST, count($this->data));
                curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
            }
        }
        else
        {
            curl_setopt($curl, CURLOPT_URL, $this->api_url);            
        }
        curl_setopt($curl, CURLOPT_REFERER, "http://code.google.com/p/api-response-cache/");
        curl_setopt($curl, CURLOPT_USERAGENT, "ApiResponseCache/1.0");
        curl_setopt($curl, CURLOPT_HEADER, 0); //remove header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //return output
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }
}

$example = new ApiResponseCache('tweet', 'http://twitter.com/statuses/user_timeline/17534556.rss');
echo $example->fetch_response();
?>